package de.fhd.pms.ctrl;

import java.util.Date;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import de.fhd.pms.dao.StationDao;
import de.fhd.pms.dao.StationDeleteException;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Kunde;
import de.fhd.pms.model.Station;

/**
 * The Controller class for {@link Station} objects. Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Fischer, Neetzel & Six
 * 
 */
@Controller
public class StationController {
	private static Logger log = Logger.getLogger(StationController.class);
	// von mir gemacht f�r StationDeleteException!
	private StationDao stationDao = null;

	public void setStationDao(StationDao stationDao) {
		this.stationDao = stationDao;
	}

	/**
	 * For every request for this controller, this will create a station
	 * instance for the form.
	 */
	// @ModelAttribute
	// public Station newRequest(@RequestParam(required=false) Integer id) {
	// return (id != null ? stationDao.findById(id) : new Station());
	// }

	/**
	 * <p>
	 * Station form request.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/EditStation.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/EditStation.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		log.debug("EditStation.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Station());
		} else {
			mv.addObject(stationDao.findById(id));
		}
		mv.setViewName("admin-edit-station");
		return mv;
	}

	/**
	 * <p>
	 * Saves a station.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveStation.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SaveStation.html", method = RequestMethod.POST)
	public String save(Station station, Model model) {
		if (station.getCreated() == null) {
			station.setCreated(new Date());
		}
		Station result = stationDao.save(station);
		// set id from create
		if (station.getId() == null) {
			station.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "station.form.msg.success");
		return "redirect:AlleStationen.html";
	}

	/**
	 * <p>
	 * Deletes a station.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/LoescheStation.html'.
	 * </p>
	 */
	// EXCEPTION VERSUCH
	// @RequestMapping(value = "/LoescheStation.html", method =
	// RequestMethod.GET)
	// public String delete(Station station) {
	// try {
	// stationDao.delete(station);
	// } catch (StationDeleteException e) {
	// e.printStackTrace();
	// }
	// return "redirect:AlleStationen.html";
	// }

	// ORIGINAL
	@RequestMapping(value = "/LoescheStation.html", method = RequestMethod.GET)
	public String delete(Station station) {
		stationDao.delete(station);
		return "redirect:AlleStationen.html";
	}

	/**
	 * <p>
	 * Searches for all stations and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AlleStationen.html'.
	 * </p>
	 */
	@RequestMapping(value = "/AlleStationen.html", method = RequestMethod.GET)
	public ModelAndView findAll() {

		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Alle Stationen der Autovermietung");
		mv.addObject("message", "Sie können hier die Stationen bearbeiten oder löschen.");
		mv.addObject("stationen", stationDao.findAll());
	

		mv.addObject("google", stationDao.getGoogle());

		mv.setViewName("admin-list-station");

		return mv;
	}

	@RequestMapping(value = "/KundenStationen.html", method = RequestMethod.GET)
	public ModelAndView findAllKundenStation() {

		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Die Stationen in München");
		mv.addObject("message", "Die Fahrzeuge sind verteilt auf alle Stationen. Sie können Fahrzeuge an allen Stationen nutzen.");
		mv.addObject("stationen", stationDao.findAll());

		mv.addObject("google", stationDao.getGoogle());

	

		mv.setViewName("kunde-list-station");

		return mv;
	}

}
